package com.kly.sc.api.infras.handler;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.kly.sc.api.infras.enums.BasicErrorCodeEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 16/8/24
 * Time: 上午11:03
 */
@SuppressWarnings("unchecked")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class ApiResponse<T> {

    private Integer code = BasicErrorCodeEnum.SUCCESS.getCode();
    
    private String msg = "SUCCESS";

    private T data;

    //private T params;

    public ApiResponse(T data) {
        this.data = data;
    }

    public ApiResponse() {
    }

    public ApiResponse(String key, T data) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, data);
        this.data = (T) map;
    }

    public ApiResponse(String key, T data, T params) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, data);
        this.data = (T) map;
        //this.params = params;
    }

    public ApiResponse(Integer errorCode, String errorMsg) {
        this.code = errorCode;
        this.msg = errorMsg;
    }

    public ApiResponse(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public ApiResponse<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ApiResponse<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public ApiResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    /*public T getParams() {
        return params;
    }

    public void setParams(T params) {
        this.params = params;
    }*/

    public static <T> ApiResponse<T> ok(T data) {
        return new ApiResponse<>(data);
    }

    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>();
    }
}
